package edu.metrostate.capstonediner;

import android.content.Intent;

public class PauseMenu {
	private final float RESUME_BUTTON_Y = 0.125f * Constants.SCREEN_WIDTH;
	private final float OPTIONS_BUTTON_Y = 2.0f * Constants.DEFAULT_FONT_SIZE + 0.25f * Constants.SCREEN_WIDTH;
	private final float QUIT_BUTTON_Y = 4.0f * Constants.DEFAULT_FONT_SIZE + 0.375f * Constants.SCREEN_WIDTH;
	
	private GameText resumeText;
	private GameText optionsText;
	private GameText quitText;
	
	private GameButton resumeButton;
	private GameButton optionsButton;
	private GameButton quitButton;
	
	private GameRenderer renderer;
	private Game game;
	private GameInput input;
	private GameActivity activity;
	
	boolean isPaused;
	
	public PauseMenu(GameActivity activity, Game game, GameInput input) {
		resumeText = new GameText("Resume Game", 88, RESUME_BUTTON_Y + 0.5f * Constants.DEFAULT_FONT_SIZE, Constants.TEXT_LAYER, Constants.DEFAULT_FONT_SIZE, Constants.DEFAULT_FONT);
		optionsText = new GameText("Options", 103, OPTIONS_BUTTON_Y + 0.5f * Constants.DEFAULT_FONT_SIZE, Constants.TEXT_LAYER, Constants.DEFAULT_FONT_SIZE, Constants.DEFAULT_FONT);
		quitText = new GameText("Quit Game", 96, QUIT_BUTTON_Y + 0.5f * Constants.DEFAULT_FONT_SIZE, Constants.TEXT_LAYER, Constants.DEFAULT_FONT_SIZE, Constants.DEFAULT_FONT);
		
		resumeButton = new GameButton(RESUME_BUTTON_Y);
		optionsButton = new GameButton(OPTIONS_BUTTON_Y);
		quitButton = new GameButton(QUIT_BUTTON_Y);
		
		this.renderer = activity.getRenderer();
		this.game = game;
		this.input = input;
		this.activity = activity;
	}
	
	public void show() {
		isPaused = true;
		
		renderer.add(resumeText);
		renderer.add(optionsText);
		renderer.add(quitText);
		
		renderer.add(resumeButton);
		renderer.add(optionsButton);
		renderer.add(quitButton);
		
		renderer.update();
		
		while (isPaused) {
			try {
				Thread.sleep(0);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			parseInput();
		}
	}
	
	private void hide() {
		isPaused = false;
		
		renderer.remove(resumeText);
		renderer.remove(optionsText);
		renderer.remove(quitText);
		
		renderer.remove(resumeButton);
		renderer.remove(optionsButton);
		renderer.remove(quitButton);
	}
	
	private void parseInput() {
		if(input.getX() < Constants.SCREEN_WIDTH - 32f) {
			// Resume
			if (input.getY() > RESUME_BUTTON_Y && input.getY() < RESUME_BUTTON_Y + resumeButton.getHeight()) {
				hide();
				game.unPause();
			}
			
			// Options
			else if (input.getY() > OPTIONS_BUTTON_Y && input.getY() < OPTIONS_BUTTON_Y + optionsButton.getHeight()) {
				hide();
				Intent i = new Intent(activity.getApplicationContext(), OptionsActivity.class);
				activity.startActivity(i);
			}
			
			// Quit
			else if (input.getY() > QUIT_BUTTON_Y && input.getY() < QUIT_BUTTON_Y + quitButton.getHeight()) {
				hide();
				game.doGameOver();
			}
		}
	}
}
